home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / mcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  3.2 KB  |  121 lines

  1. /* Standard debugging hooks for `malloc'.
  2.    Copyright 1990 Free Software Foundation
  3.           Written May 1989 by Mike Haertel.
  4.  
  5.    The author may be reached (Email) at the address mike@ai.mit.edu,
  6.    or (US mail) as Mike Haertel c/o Free Software Foundation.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "ansidecl.h"
  23. #include "gmalloc.h"
  24.  
  25. /* Old hook values.  */
  26. static void EXFUN((*old_free_hook), (PTR ptr));
  27. static PTR EXFUN((*old_malloc_hook), (size_t size));
  28. static PTR EXFUN((*old_realloc_hook), (PTR ptr, size_t size));
  29.  
  30.  
  31. /* Function to call when something awful happens. */
  32. extern void abort();
  33. static void EXFUN((*abortfunc), (void)) = (void (*)()) abort;
  34.  
  35. /* Arbitrary magical numbers.  */
  36. #define MAGICWORD    0xfedabeeb
  37. #define MAGICBYTE    ((char) 0xd7)
  38.  
  39. struct hdr
  40.   {
  41.     size_t size;        /* Exact size requested by user.  */
  42.     unsigned int magic;        /* Magic number to check header integrity.  */
  43.   };
  44.  
  45. static void
  46. DEFUN(checkhdr, (hdr), CONST struct hdr *hdr)
  47. {
  48.   if (hdr->magic != MAGICWORD || ((char *) &hdr[1])[hdr->size] != MAGICBYTE)
  49.     (*abortfunc)();
  50. }
  51.  
  52. static void
  53. DEFUN(freehook, (ptr), PTR ptr)
  54. {
  55.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  56.   checkhdr(hdr);
  57.   hdr->magic = 0;
  58.   __free_hook = old_free_hook;
  59.   free(hdr);
  60.   __free_hook = freehook;
  61. }
  62.  
  63. static PTR
  64. DEFUN(mallochook, (size), size_t size)
  65. {
  66.   struct hdr *hdr;
  67.  
  68.   __malloc_hook = old_malloc_hook;
  69.   hdr = (struct hdr *) malloc(sizeof(struct hdr) + size + 1);
  70.   __malloc_hook = mallochook;
  71.   if (hdr == NULL)
  72.     return NULL;
  73.  
  74.   hdr->size = size;
  75.   hdr->magic = MAGICWORD;
  76.   ((char *) &hdr[1])[size] = MAGICBYTE;
  77.   return (PTR) (hdr + 1);
  78. }
  79.  
  80. static PTR
  81. DEFUN(reallochook, (ptr, size), PTR ptr AND size_t size)
  82. {
  83.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  84.  
  85.   checkhdr(hdr);
  86.   __free_hook = old_free_hook;
  87.   __malloc_hook = old_malloc_hook;
  88.   __realloc_hook = old_realloc_hook;
  89.   hdr = (struct hdr *) realloc((PTR) hdr, sizeof(struct hdr) + size + 1);
  90.   __free_hook = freehook;
  91.   __malloc_hook = mallochook;
  92.   __realloc_hook = reallochook;
  93.   if (hdr == NULL)
  94.     return NULL;
  95.  
  96.   hdr->size = size;
  97.   ((char *) &hdr[1])[size] = MAGICBYTE;
  98.   return (PTR) (hdr + 1);
  99. }
  100.  
  101. void
  102. DEFUN(mcheck, (func), void EXFUN((*func), (void)))
  103. {
  104.   static int mcheck_used = 0;
  105.  
  106.   if (func)
  107.     abortfunc = func;
  108.  
  109.   /* These hooks may not be safely inserted if malloc is already in use.  */
  110.   if (!__malloc_initialized && !mcheck_used)
  111.     {
  112.       old_free_hook = __free_hook;
  113.       __free_hook = freehook;
  114.       old_malloc_hook = __malloc_hook;
  115.       __malloc_hook = mallochook;
  116.       old_realloc_hook = __realloc_hook;
  117.       __realloc_hook = reallochook;
  118.       mcheck_used = 1;
  119.     }
  120. }
  121.